package fr.insta.webview.service;

import fr.insta.browser.domain.IHistoryEntry;
import fr.insta.webview.object.IWebObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>ServiceWebObject class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public enum ServiceWebObject
{
	INSTANCE;
	
	/**
	 * <p>serializeHistory.</p>
	 *
	 * @param _e a {@link fr.insta.browser.domain.IHistoryEntry} object.
	 */
	public void serializeHistory(IHistoryEntry _e)
	{
		serialize("History", ".history", _e);
	}
	
	/**
	 * <p>unserializeHistory.</p>
	 *
	 * @return a {@link java.util.List} object.
	 */
	public List<IWebObject> unserializeHistory() {
		return unserialize("History", ".history");
	}
	
	/**
	 * <p>serializeBookmark.</p>
	 *
	 * @param _iWebObject a {@link fr.insta.webview.object.IWebObject} object.
	 */
	public void serializeBookmark(IWebObject _iWebObject) {
		serialize("Bookmark", ".bookmark", _iWebObject);
	}
	
	/**
	 * <p>unserializeBookmarks.</p>
	 *
	 * @return a {@link java.util.List} object.
	 */
	public List<IWebObject> unserializeBookmarks() {
		return unserialize("Bookmark", ".bookmark");
	}
	
	/**
	 * <p>serializeHomepage.</p>
	 *
	 * @param _iWebObject a {@link fr.insta.webview.object.IWebObject} object.
	 */
	public void serializeHomepage(IWebObject _iWebObject) {
		serialize("Homepage", ".home", _iWebObject);
	}
	
	/**
	 * <p>unserializeHomepage.</p>
	 *
	 * @return a {@link java.util.List} object.
	 */
	public List<IWebObject> unserializeHomepage() {
		return unserialize("Homepage", ".home");
	}

	private void serialize(String _dir, String _extension, IWebObject _webObject) {
		try{
			File bookmarkDir = new File(_dir);
			
			if(!bookmarkDir.exists())
				bookmarkDir.mkdir();
			
			if("Homepage".equals(_dir) && bookmarkDir.listFiles().length != 0)
			{
				for(File fichier : bookmarkDir.listFiles())
				{
					fichier.delete();
				}
			}
			
			FileOutputStream fichier = new FileOutputStream(bookmarkDir + "/" + _webObject.getDate().getTime() + _extension);
			
			ObjectOutputStream oos = new ObjectOutputStream(fichier);
			oos.writeObject(_webObject);
			oos.flush();
			oos.close();
		}
		catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}
	
	private List<IWebObject> unserialize(String _dir, String _extensionExpected) {
		
		ArrayList<IWebObject> listWebObject = new ArrayList<IWebObject>();
		
		try
		{
			File file = new File(_dir);
			
			if(file.isDirectory()) {
				File[] tabWebObject = file.listFiles();
				
				for (File webObjectFile : tabWebObject)
				{
					//TODO test the extension substring
					String fileName = webObjectFile.getName();
					String extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
					
					if(extension != null && extension.contentEquals(_extensionExpected)) {
						
						FileInputStream fichier = new FileInputStream(webObjectFile);
						ObjectInputStream ois = new ObjectInputStream(fichier);
						IWebObject webObject = (IWebObject) ois.readObject();
						
						fichier.close();
						ois.close();
						
						listWebObject.add(webObject);
					}
				}
			}
		}
		catch (IOException | ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		
		return listWebObject;
	}
}
